package yunwo.cn.yzstation.model;

import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.IcarOrdModel;
import yunwo.cn.yzstation.api.okapi.GLCallBack;
import yunwo.cn.yzstation.bean.EndOrganbean;
import yunwo.cn.yzstation.bean.OrderinfodetailsBean;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.presenter.CarOrderDetailsPersenter;
import yunwo.cn.yzstation.utils.StringUtil;
import yunwo.cn.yzstation.view.activity.CarOrderDetailsActivity;

/**
 * Created by lao on 2017/10/16.
 */
public class CarordModel {
    CarOrderDetailsActivity carOrderDetailsActivity;
    IcarOrdModel icarOrdModel;
    public CarordModel(CarOrderDetailsActivity carOrderDetailsActivity, IcarOrdModel icarOrdModel) {
        this.carOrderDetailsActivity = carOrderDetailsActivity;
        this.icarOrdModel = icarOrdModel;

    }

    public void getData(String id) {

        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        HttpManager.hpptPostString3(MyConstants.arrvalGoods,map).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    if (!StringUtil.isEmpty(result)) {
                        List<OrderinfodetailsBean.DataBean> dataBeanList = new Gson().fromJson(result, new TypeToken<List<OrderinfodetailsBean.DataBean>>() {
                        }.getType());
                        icarOrdModel.getgetDataSuccess(dataBeanList);
                    }
                }else {
                    Toast.makeText(carOrderDetailsActivity, msg, Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(carOrderDetailsActivity, "请求失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void submit(List<OrderinfodetailsBean.DataBean> selectedCollectedCars) {
        Map<String, Object> map = new HashMap<>();
        map.put("params", selectedCollectedCars);
        HttpManager.hpptPostObj(MyConstants.confirmGoods,map).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    Toast.makeText(carOrderDetailsActivity, "到货成功", Toast.LENGTH_SHORT).show();
                    icarOrdModel.submitSuccess();
                }else {
                    Toast.makeText(carOrderDetailsActivity, "到货失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(carOrderDetailsActivity, "服务器异常", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
